package com.siysoft.tc.friend.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.siysoft.tc.base.bean.bo.ClassroomShortBo;
import com.siysoft.tc.base.bean.bo.FriendBo;
import com.siysoft.tc.base.bean.bo.PaginationBo;
import com.siysoft.tc.friend.service.IFriendService;
import com.siysoft.util.COMMON;
import com.tc.base.controller.BaseController;

@Controller
@RequestMapping(value = "/app/friend")
public class FriendController extends BaseController{
	@Autowired
	IFriendService friendService;
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/loading")
	public void loading(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<FriendBo> friend = friendService.getfriend(id, pb);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("friend", friend);
			obj.accumulate("pagination", pb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("head_id"))
					|| COMMON.isnull(request.getParameter("dependent_id"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer head_id = Integer.valueOf(request.getParameter("head_id").toString());
			Integer dependent_id = Integer.valueOf(request.getParameter("dependent_id").toString());
			
			String result = friendService.delete(head_id, dependent_id);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("result", result);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/searchuser")
	public void searchuser(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("name"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			String name = request.getParameter("name").toString();
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<FriendBo> user = friendService.searchUser(name, pb);
			
			if(COMMON.isEmpty(user)){
				this.responseFail(response, -32602, "该用户不存在！", "");
				return ;
			}
			
			JSONObject obj = new JSONObject();
			obj.accumulate("user", user);
			obj.accumulate("pagination", pb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/add")
	public void add(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("head_id"))
					|| COMMON.isnull(request.getParameter("dependent_id"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer head_id = Integer.valueOf(request.getParameter("head_id").toString());
			Integer dependent_id = Integer.valueOf(request.getParameter("dependent_id").toString());
			
			String result = friendService.add(head_id, dependent_id);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("result", result);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/inviteclassroom")
	public void inviteclassroom(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<ClassroomShortBo> classroom_short = friendService.getClassroomShort(id, pb);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("classroom_short", classroom_short);
			obj.accumulate("pagination", pb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/seachclassroom")
	public void seachclassroom(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("number"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			String number = request.getParameter("number").toString();
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<ClassroomShortBo> classroom_short = friendService.searchClassroom(number, pb);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("classroom_short", classroom_short);
			obj.accumulate("pagination", pb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/invite")
	public void invite(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("number"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			String number = request.getParameter("number").toString();
			
			Integer result = friendService.invite(id, number);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("result", result);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
}
